package com.example.smartcity1.service.parklot;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.example.smartcity1.R;
import com.example.smartcity1.adapter.ParklotListAdapter;
import com.example.smartcity1.adapter.ParklotOldAdapter;
import com.example.smartcity1.bean.ParklotListBean;
import com.example.smartcity1.bean.ParklotOldListBean;
import com.example.smartcity1.network.RetrofitClient;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ParklotOldListActivity extends AppCompatActivity {

    private EditText entryTimeEdit;
    private EditText outTimeEdit;
    private ImageButton searchBtn;
    private ListView listView;
    private Button button;

    private List<ParklotOldListBean.RowsDTO> rows = new ArrayList<>();
    private ParklotOldAdapter adapter;

    private int size = 1;
    private String entryTime = "";
    private String outTime = "";
    private Boolean allOK = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_parklot_old_list);
        ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar != null) {
            supportActionBar.setTitle("停车记录");
            supportActionBar.setDisplayHomeAsUpEnabled(true);
        }
        initView();
        adapter = new ParklotOldAdapter(ParklotOldListActivity.this, R.layout.item_parkolt_old, rows);
        listView.setAdapter(adapter);
        setData();
        button.setOnClickListener(v -> {
            if (!allOK) {
                size += 1;
                setData();
            } else {
                Toast.makeText(this, "已全部加载", Toast.LENGTH_SHORT).show();
            }
        });
        searchBtn.setOnClickListener(v -> {
            if (entryTimeEdit.getText().toString().equals("") || outTimeEdit.getText().toString().equals("")) {
                Toast.makeText(this, "请检查时间", Toast.LENGTH_SHORT).show();
            } else {
                entryTime = entryTimeEdit.getText().toString();
                outTime = outTimeEdit.getText().toString();
                size = 1;
                allOK = false;
                rows.clear();
                setData();
            }
        });
    }

    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return super.onSupportNavigateUp();
    }


    private void initView() {
        entryTimeEdit = findViewById(R.id.entryTime_edit);
        outTimeEdit = findViewById(R.id.outTime_edit);
        searchBtn = findViewById(R.id.search_btn);
        listView = findViewById(R.id.listView);
        button = findViewById(R.id.button);
    }

    private void setData() {
        RetrofitClient.serviceService().getParklotOldListData(size,entryTime,outTime).enqueue(new Callback<ParklotOldListBean>() {
            @Override
            public void onResponse(Call<ParklotOldListBean> call, Response<ParklotOldListBean> response) {
                ParklotOldListBean body = response.body();
                if (body != null && body.getCode() == 200) {
                    rows.addAll(body.getRows());
                    adapter.notifyDataSetChanged();
                    if (rows.size() == body.getTotal()) {
                        allOK = true;
                    }
                } else {
                    Toast.makeText(ParklotOldListActivity.this, body.getMsg(), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<ParklotOldListBean> call, Throwable t) {

            }
        });
    }
}